﻿#ifndef SERVERMONITORTHREAD_H
#define SERVERMONITORTHREAD_H
#include <QMutex>
#include <QThread>
#include <QTimer>
#include <cpuinfo.h>
#include <diskinfo.h>
#include <diskuse.h>
#include <memoryinfo.h>
#include <networkinfo.h>
#include <networkuse.h>
#include <qnetworktrafficmonitor.h>

#include "cpuuse.h"
#include "memoryuse.h"
#include "qcpumonitor.h"
#include "qmemorymonitor.h"
#include "serverinfo.h"

class ServerMonitorCenter : public QThread
{
public:
    /**
    * 获取单例实例
    * @return 返回单例实例
    */
    static ServerMonitorCenter &getInstance();
    /**
    * 删除单例实例
    */
    static void deleteInstance();

    void getServerInfo(ServerInfo &info);

    void getCpuInfoList(QList<CpuInfo> &list);

    void getMemoryInfoList(QList<MemoryInfo> &list);

    void getDiskInfoList(QList<DiskInfo> &list);

    void getCpuUseList(QList<CpuUse> &list);

    void getMemoryUseList(QList<MemoryUse> &list);

    void getVirtualMemoryUseList(QList<MemoryUse> &list);

    void getDiskUseList(QList<DiskUse> &list);

    void getNetworkInfoList(QList<NetworkInfo> &list);

    void getNetworkUseList(QList<NetworkUse> &list);

    DiskUse getDiskUseAll();

private:
    ServerMonitorCenter(QObject *parent = nullptr);
    ~ServerMonitorCenter();

    static ServerMonitorCenter *instance;
    static QMutex mutex;
private slots:
    void slot_updateInfo();

private:
    ServerInfo serverInfo; /**< 服务器信息 */

    QList<CpuInfo> cpuInfoList;
    QMutex cpuUseListMutex;
    QList<CpuUse> cpuUseList; /**< Cpu使用率记录列表*/

    QList<MemoryInfo> memoryInfoList;
    QMutex memoryUseListMutex;
    QList<MemoryUse> memoryUseList; /**< 内存使用率记录列表*/

    QMutex virtualMemoryUseListMutex;
    QList<MemoryUse> virtualMemoryUseList; /**< 虚拟内存使用率记录列表*/

    QMutex diskInfoListMutex;
    QList<DiskInfo> diskInfoList;
    QMutex diskUseListMutex;
    QList<DiskUse> diskUseList;

    QMutex diskUseAllMutex;
    DiskUse diskUseAll; /**< 磁盘总共使用情况*/

    QMutex networkInfoListMutex;
    QList<NetworkInfo> networkInfoList; /**< 网络适配器列表*/
    QMutex networkUseListMutex;
    QList<NetworkUse> networkUseList; /**< 网络使用记录列表*/
private:
    QCpuMonitor cpuMonitor;
    QMemoryMonitor memoryMonitor;
    QNetworkTrafficMonitor networkTrafficMonitor;

protected:
    void run() override;
};

#endif // SERVERMONITORTHREAD_H
